package top.jiangqiang.qyblog.server.config;

import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;

/**
 * @author Jiangqiang
 * @version 1.0
 * @date 2022/12/9 14:03
 */
@Configuration
public class DocConfiguration {
    @Bean
    public GroupedOpenApi publicApi() {
        return GroupedOpenApi.builder()
                .group("公开接口")
                .packagesToScan("top.jiangqiang.qyblog")
                .pathsToMatch("/public/**")
                .build();
    }

    @Bean
    public GroupedOpenApi adminApi() {
        return GroupedOpenApi.builder()
                .group("后台接口")
                .pathsToMatch("/admin/**")
                .packagesToScan("top.jiangqiang.qyblog.admin.controller")
                .build();
    }

    @Bean
    public OpenAPI qianYiOpenApi() {
        return new OpenAPI()
                .info(new Info().title("千异博客接口文档")
                        .description("千异博客接口文档")
                        .version("v1.0.0")
                        .license(new License().name("GPL-3.0").url("https://www.gnu.org/licenses/gpl-3.0.html")))
                .schemaRequirement(HttpHeaders.AUTHORIZATION, securityScheme())
                //全局安全校验项，也可以在对应的controller上加注解SecurityRequirement
                .addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION))
                .externalDocs(new ExternalDocumentation()
                        .description("千异博客")
                        .url("https://www.jiangqiang.top"));
    }

    private SecurityScheme securityScheme() {
        SecurityScheme securityScheme = new SecurityScheme();
        //类型
        securityScheme.setType(SecurityScheme.Type.APIKEY);
        //请求头的name
        securityScheme.setName(HttpHeaders.AUTHORIZATION);
        //token所在位置
        securityScheme.setIn(SecurityScheme.In.HEADER);
        securityScheme.setBearerFormat("Bearer ");
        return securityScheme;
    }
}
